package cn.onecloud.tablet.xiaozhi;

import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.onecloud.tablet.xiaozhi.model.DesktopInfo;

/**
 * Created by yin.he on 12/5/17.
 */

public class LoginUserMsg {

    private static final String APPEND_DIR = File.separator + "userMessage";
    private static final String FILE_NAME = "message.xml";

    public static List<DesktopInfo> xmlRead(File filesDir) {
        String path = filesDir + APPEND_DIR;
        XmlPullParser parser = Xml.newPullParser();
        List<DesktopInfo> desktopInfos = new ArrayList<DesktopInfo>();
        InputStream inputStream = null;
        try {
            File file = new File(path, FILE_NAME);
            inputStream = new FileInputStream(file);
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();
            DesktopInfo user = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("user")) {
                            user = new DesktopInfo();
                        } else if (user != null) {
                            if (name.equalsIgnoreCase("address")) {
                                user.setIp(parser.nextText());
                            } else if (name.equalsIgnoreCase("port")) {
                                user.setPort(parser.nextText());
                            } else if (name.equalsIgnoreCase("domain")) {
                                user.setDomain(parser.nextText());
                            } else if (name.equalsIgnoreCase("username")) {
                                user.setUsername(parser.nextText());
                            } else if (name.equalsIgnoreCase("password")) {
                                user.setPassword(parser.nextText());
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (parser.getName().equalsIgnoreCase("user") && user != null) {
                            desktopInfos.add(user);
                            user = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            return desktopInfos;
        } catch (FileNotFoundException e) {
            Log.e("message.xml read!", e.toString());
        } catch (Exception e) {
            Log.e("xml read error!", e.toString());
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                Log.e("IO close error!", e.toString());
            }
        }
        return desktopInfos;
    }


    public static void xmlWrite(List<DesktopInfo> desktopInfoList, File filesDir) {
        String path = filesDir + APPEND_DIR;
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        FileWriter fileWriter = null;
        try {
            File file = new File(path, FILE_NAME);
            if (!file.exists()) {
                file.createNewFile();
            }
            XmlSerializer serializer = Xml.newSerializer();
            fileWriter = new FileWriter(file);
            serializer.setOutput(fileWriter);
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "desktopInfo");
            for (int i = 0; i < desktopInfoList.size(); i++) {
                DesktopInfo user = desktopInfoList.get(i);
                serializer.startTag(null, "user");
                serializer.startTag(null, "address");
                serializer.text(user.getIp());
                serializer.endTag(null, "address");

                serializer.startTag(null, "port");
                serializer.text(user.getPort());
                serializer.endTag(null, "port");

                serializer.startTag(null, "domain");
                serializer.text(user.getDomain());
                serializer.endTag(null, "domain");

                serializer.startTag(null, "username");
                serializer.text(user.getUsername());
                serializer.endTag(null, "username");

                serializer.startTag(null, "password");
                serializer.text(user.getPassword());
                serializer.endTag(null, "password");

                serializer.endTag(null, "user");
            }
            serializer.endTag(null, "desktopInfo");
            serializer.endDocument();
            fileWriter.flush();
        } catch (IOException e) {
            Log.e("message.xml write!", e.toString());
        } finally {
            try {
                if (null != fileWriter) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                Log.e("IO close error!", e.toString());
            }
        }
    }
}
